package graph.group.zhangtao.guider.getter;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

import graph.group.zhangtao.guider.utils.MySettingsAndTools;

/**
 * 获取景点的json,通用接口，完成后动作由Runnable决定
 * Created by Me on 2017/12/25.
 */

public class TextGetterFactory extends AsyncTask {
    private Context context;
    private String path;

    /**
     * @param context
     * @param path
     */
    private TextGetterFactory(Context context, String path) {
        this.context = context;
        this.path = path;

    }

    @Override
    protected Object doInBackground(Object[] objects) {
        String result = "";
        try {
            result = MySettingsAndTools.getHtml(path);
        } catch (Exception e) {
            e.printStackTrace();
            result = "wrong";
        }
        return result;
    }

    /**
     * 获取网页文本的静态方法
     *
     * @param context    上下文
     * @param path       跟路径
     * @param id         景点id
     * @param extendPath 内部地址
     * @return 获取到的json文本
     */
    public static String textGet(Context context, String path, String id, String extendPath, boolean fromfile) {
        if (fromfile) {
            path = path + id + "/" + extendPath;
            Scanner scanner = null;
            StringBuilder stringBuilder = new StringBuilder();
            try {
                scanner = new Scanner(
                        new BufferedInputStream(
                                new FileInputStream(path)
                        )
                );
                while (scanner.hasNextLine()) {
                    stringBuilder.append(scanner.nextLine());
                }
                Log.w("tao",stringBuilder.toString());

                return stringBuilder.toString();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return MySettingsAndTools.getWRONG();
            } finally {
                if (scanner != null)
                    scanner.close();
            }
        } else {
            try {
                String tempId = URLEncoder.encode(id, "UTF-8");
                path = path + tempId + "/" + extendPath;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            try {
                Object mResult = new TextGetterFactory(context,
                        path).execute().get();
                return (String) mResult;
            } catch (InterruptedException e) {

            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            return MySettingsAndTools.getWRONG();
        }
    }

    public static String textGet(Context context, String path, boolean fromfile) {

        if (fromfile) {
            Scanner scanner = null;
            StringBuilder stringBuilder = new StringBuilder();
            try {
                scanner = new Scanner(
                        new BufferedInputStream(
                                new FileInputStream(path)
                        )
                );
                while (scanner.hasNextLine()) {
                    stringBuilder.append(scanner.nextLine());
                }
                return stringBuilder.toString();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return MySettingsAndTools.getWRONG();
            } finally {
                if (scanner != null)
                    scanner.close();
            }
        } else {
            try {
                Object mResult = new TextGetterFactory(context,
                        path).execute().get();
                return (String) mResult;
            } catch (InterruptedException e) {

            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            return MySettingsAndTools.getWRONG();
        }

    }

}
